package QuanLyDonViTinh;

import KetNoiSQL.KetNoiSQL;
import KiemTra.KiemTra;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class DanhSachDonViTinh extends javax.swing.JPanel {

    private DefaultTableModel modelDonViTinh = new DefaultTableModel();
    String tenBang = "DonViTinh";
    Vector vLay = new Vector();

    public DanhSachDonViTinh() {
        initComponents();
        try {
            vLay = DonViTinh.xemDonViTinh();
            this.taiBangDonViTinh(tenBang, vLay);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    //Lấy dữ liệu vào bảng
    public void taiBangDonViTinh(String tenBang, Vector vLay) throws SQLException {
        modelDonViTinh.setNumRows(0);
        Vector cotDVTinh = new Vector();
        cotDVTinh.addElement("Mã DVT");
        cotDVTinh.addElement("Tên DVT");
        modelDonViTinh.setColumnIdentifiers(cotDVTinh);
        DonViTinh dvt = new DonViTinh();
        for (int i = 0; i < vLay.size(); i++) {
            dvt = (DonViTinh) vLay.get(i);
            Vector hangDVTinh = new Vector();
            hangDVTinh.addElement(dvt.getMaDonViTinh());
            hangDVTinh.addElement(dvt.getTenDonViTinh());
            modelDonViTinh.addRow(hangDVTinh);
        }
        tbDonViTinh.setModel(modelDonViTinh);
    }
    //Kiểm tra mã đơn vị tính 

    public boolean kiemTraMaDVT() {
        String ma = txtMa.getText().trim();
        boolean kt = false;
        try {
            ResultSet rs = KetNoiSQL.thucHienTruVan("spXemDVTinh");
            while (rs.next()) {
                if (ma.equalsIgnoreCase(rs.getString("MaDVTinh"))) { //so sánh k phân biệt hoa thường
                    txtMa.setText("");
                    txtMa.requestFocus();
                    kt = true;
                }
            }
            if (kt == true) {
                JOptionPane.showMessageDialog(this, "Mã loại đơn vị tính đã tồn tại.Nhập vào mã khác !", "Thông báo!", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return kt;
    }
    //Kiểm tra cú pháp 

    public boolean ktraDVT() {
        int kt = 0;
        if (txtMa.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Bạn phải nhập vào mã đơn vị tính.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            txtMa.setText("");
            txtMa.requestFocus();
            return false;
        }else if (txtTen.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Bạn phải nhập vào tên đơn vị tính.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            txtTen.setText("");
            txtTen.requestFocus();
            return false;
        }
        return true;
    }
    //Thêm Đơn vị tính

    public void themDonViTinh() {
        if (kiemTraMaDVT() || !ktraDVT()) {
            return;
        }
        int kt = 0;
        DonViTinh dvt = new DonViTinh();
        dvt.setMaDonViTinh(txtMa.getText().trim());
        dvt.setTenDonViTinh(txtTen.getText().trim());
        try {
            kt = DonViTinh.themDonViTinh(dvt);
            if (kt == 1) {
                JOptionPane.showMessageDialog(this, "Thêm đơn vị tính thành công !", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                vLay = DonViTinh.xemDonViTinh();
                this.taiBangDonViTinh(tenBang, vLay);
            } else {
                JOptionPane.showMessageDialog(this, "Thêm đơn vị tính không thành công.Xem lại", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    //Cập nhật đơn vị tính

    public void capNhatDonViTinh() {
        int ktra = 0;
        if (!ktraDVT()) {
            return;
        }
        DonViTinh dvt = new DonViTinh();
        dvt.setMaDonViTinh(txtMa.getText().trim());
        dvt.setTenDonViTinh(txtTen.getText().trim());
        try {
            ktra = DonViTinh.capNhatDVTinh(dvt);
            if (ktra == 1) {
                JOptionPane.showMessageDialog(this, "Cập nhật thành công!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                vLay = DonViTinh.xemDonViTinh();
                this.taiBangDonViTinh(tenBang, vLay);
            } else {
                JOptionPane.showMessageDialog(this, "Cập nhật không thành công.Không thể thay đổi mã đơn vị tính !", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    //Xóa đơn vị tính

    public void xoaDonViTinh() {
        int show = JOptionPane.showConfirmDialog(this, "Bạn có muốn xóa đơn vị tính ?", "Thông báo", JOptionPane.YES_NO_OPTION);
        if (show == JOptionPane.YES_OPTION) {
            int kt = 0, dem = 0;
            int[] rArr = tbDonViTinh.getSelectedRows();
            try {
                for (int i = 0; i < rArr.length; i++) {
                    kt = DonViTinh.xoaDVTinh(tbDonViTinh.getValueAt(rArr[i], 1).toString());
                    if (kt == 1) {
                        dem++;
                    }
                }
                if (dem > 0) {
                    JOptionPane.showMessageDialog(this, "Xóa " + dem + " đơn vị tính", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                    vLay = DonViTinh.xemDonViTinh();
                    this.taiBangDonViTinh(tenBang, vLay);
                } else {
                    JOptionPane.showMessageDialog(this, "Xóa đơn vị tính bị lỗi.Đếm = " + dem + " !", "Thông báo", JOptionPane.ERROR_MESSAGE);
                }
            } catch (SQLException ex) {
                System.err.println(ex.getMessage());
            }
        }
    }
    //Làm lại

    public void resetForm() {
        txtMa.setText("");
        txtTen.setText("");
        txtMa.requestFocus();
    }
    //Tìm kiếm

    public void timKiemDonViTinh() throws SQLException {

        Vector vlist = new Vector();
        if (txtMa.getText().equals("") && txtTen.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Bạn phải nhập vào Mã hoặc tên Đơn vị tính cần tìm", "Thông Báo", JOptionPane.OK_OPTION);
            vLay = DonViTinh.xemDonViTinh();
            this.taiBangDonViTinh(tenBang, vLay);
        } else if (txtMa.getText() != "" && txtTen.getText().equals("")) {
            vlist.addElement(txtMa.getText().trim());
            vLay = DonViTinh.timKiemMaDonViTinh(vlist);
            this.taiBangDonViTinh(tenBang, vLay);
        } else if (txtMa.getText().equals("") && txtTen.getText() != "") {
            vlist.addElement(txtTen.getText().trim());
            vLay = DonViTinh.timKiemTenDonViTinh(vlist);
            this.taiBangDonViTinh(tenBang, vLay);
        } else {
            vlist.addElement(txtMa.getText().trim());
            vlist.addElement(txtTen.getText().trim());
            vLay = DonViTinh.timKiemMa_TenDonViTinh(vlist);
            this.taiBangDonViTinh(tenBang, vLay);
        }
    }

    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDonViTinh = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        txtMa = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtTen = new javax.swing.JTextField();
        btTimKiem = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        btThem = new javax.swing.JButton();
        btXoa = new javax.swing.JButton();
        btCapNhat = new javax.swing.JButton();
        btTroLai = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách loại thuốc"));

        tbDonViTinh.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbDonViTinh.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDonViTinhMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbDonViTinh);

        jLabel2.setText("Loại thuốc ID");

        jLabel3.setText("Tên loại thuốc");

        btTimKiem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/Search.png"))); // NOI18N
        btTimKiem.setText("Tìm kiếm");
        btTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTimKiemActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/234.png"))); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(txtMa, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(txtTen, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(67, 67, 67)
                        .addComponent(btTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 534, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 129, Short.MAX_VALUE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(42, 42, 42))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(42, 42, 42)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btTimKiem)
                    .addComponent(txtTen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txtMa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addContainerGap(73, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btTimKiem, jLabel2, jLabel3, txtMa, txtTen});

        btThem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/add.png"))); // NOI18N
        btThem.setText("Thêm");
        btThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThemActionPerformed(evt);
            }
        });

        btXoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/cross.png"))); // NOI18N
        btXoa.setText("Xóa");
        btXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXoaActionPerformed(evt);
            }
        });

        btCapNhat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/edit.png"))); // NOI18N
        btCapNhat.setText("Cập Nhật");
        btCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCapNhatActionPerformed(evt);
            }
        });

        btTroLai.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/reset.png"))); // NOI18N
        btTroLai.setText("Trở lại");
        btTroLai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTroLaiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(btThem, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41)
                .addComponent(btCapNhat)
                .addGap(46, 46, 46)
                .addComponent(btXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(49, 49, 49)
                .addComponent(btTroLai, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(195, 195, 195))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btTroLai, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btCapNhat, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btThem, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(78, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tbDonViTinhMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDonViTinhMouseClicked
        // TODO add your handling code here:
        int r = tbDonViTinh.getSelectedRow();
        if (r < 0) {
            return;
        }
        txtMa.setText("" + tbDonViTinh.getValueAt(r, 0));
        txtTen.setText("" + tbDonViTinh.getValueAt(r, 1));
    }//GEN-LAST:event_tbDonViTinhMouseClicked

    private void btThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemActionPerformed
        this.themDonViTinh();
        this.resetForm();
    }//GEN-LAST:event_btThemActionPerformed

    private void btCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCapNhatActionPerformed
        this.capNhatDonViTinh();
        this.resetForm();
    }//GEN-LAST:event_btCapNhatActionPerformed

    private void btXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoaActionPerformed
        this.xoaDonViTinh();
        this.resetForm();
    }//GEN-LAST:event_btXoaActionPerformed

    private void btTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTimKiemActionPerformed
        try {
            this.timKiemDonViTinh();
        } catch (SQLException ex) {
            Logger.getLogger(DanhSachDonViTinh.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btTimKiemActionPerformed

    private void btTroLaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTroLaiActionPerformed
      this.resetForm();
        try {
            vLay = DonViTinh.xemDonViTinh();
        } catch (SQLException ex) {
            Logger.getLogger(DanhSachDonViTinh.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            this.taiBangDonViTinh(tenBang, vLay);
        } catch (SQLException ex) {
            Logger.getLogger(DanhSachDonViTinh.class.getName()).log(Level.SEVERE, null, ex);
        }
      
    }//GEN-LAST:event_btTroLaiActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCapNhat;
    private javax.swing.JButton btThem;
    private javax.swing.JButton btTimKiem;
    private javax.swing.JButton btTroLai;
    private javax.swing.JButton btXoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbDonViTinh;
    private javax.swing.JTextField txtMa;
    private javax.swing.JTextField txtTen;
    // End of variables declaration//GEN-END:variables
}
